%%Paramter values
%%Calibrate epsbar to match eps
%%Calibrate Q to match Q^sig-1T from graviuty, computing T from Frobenius


clc
clear all
close all

%%Parameter values
M=3; %%Number of countries

%%Country 3 is China (tariff data from CEPII). Alternatively one could get
%%these data for UNCTAD. I focus on hs2-80


tau = [
0	3.1765687	4.5798633
14.135589	0	13.164041
18.653993	15.845213 0	
]./100;

for i=1:M
tau(i,i)=0;
end


%%PPML with domestic trade flows following referee 2 suggestion

distT=[
1	1.9783412	1.8109497
2.6752963	1	2.3168859
3.6139813	2.8582369 1	
]./(1+tau);



distNT=distT;


for k=1:M
   distT(k,k)=1; 
   distNT(k,k)=1; 
end


sig=5; %%Elasticity of substitution: sig-1=theta from EK
mbar=sig/(sig-1); %%Mark-up
L=[0.23 1.82 1]'; %%Population relative to China
L=L./L(M);


%%Sectoral parameters

alpha=0.99999.*[0.45 0.45 0.45]./0.45; %%There is only one sector in this version
LT=alpha'.*L;
LNT=(1-alpha').*L;

beta=0.985;  %%Discount factor (r around 3%)

%%Low IPR enforcement means the ocuntry pays only 10% instead of 20%

%%Initial guess of adoption spending

%Ha=0.01.*[0.001 0.001 0.001;0.001 0.001 0.001;0.001 0.001 0.001].*100;
Ha=[0.0067    0.0032    0.0021;
    0.0162    0.0015    0.0023;
    0.0091    0.0021    0.0006];


%%Updating g and T
g = [0.02 0.05]; %%Initial guess
ii=2;
tolgT=10^(-5);
diffgT=10;

Tnew=[1
    1
    1];

gg=0.04;





Bbar=[0 0 0];
eta=0.001;

w=[15
    8
    1.0000
];


gg=0.074;
 
betar = 0.4661;
betaa=betar;

ipr = [1 1 1];
ipr_rho = [1 1 0.4]; %%IPR enforcement from royalty paymnets gravity regression
gam_barg = 1;
rho = 0.25.*ipr_rho.^gam_barg; %%royalty fee following 25% rule and including IPR quality

%%Gravity for trade flows

TFP =[
18.560357
5.9865172
1
].^4;



distNT=distT;
w = TFP.^0.25;
Hrdata = [2.62 1.89 0.89]./100; %%R&D intensity data

TNT=[1 1 1];
lamNT=[1 1 1];

%%Probability of adoption estimated from gravity equation for royalty
%%payments setting the domestic diffusion speed to 0.5


eps=[
	0.5 .2823457	.3326161
.343123	0.5	.1478365
.282302	.3273594 0.5	
];



save('Parameters');